import Common from '../../../public/actions/employing/common';

let currentOptSelectedRows = null;

export default class OfferAction extends Common{
    constructor(comp) {
        super();
        this.comp = comp;
    }

    // 打开offer弹窗
    openOfferModal = async (selectedRows) => {
        let content = await this.getOfferContent(selectedRows);
        currentOptSelectedRows = selectedRows;
        if(content) {
            this.update({
                offerContentModalVisible: true,
                offerModalContent: content
            });
        }
    }

    // 关闭offer弹窗
    closeOfferModal = () => {
        currentOptSelectedRows = null;

        this.update({
            offerContentModalVisible: false,
            offerModalContent: ''
        });
    }

    // 发送offer
    sendOffer = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        if(!selectedRows && currentOptSelectedRows) {
            selectedRows = currentOptSelectedRows;
        }

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/sendOffer',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                this.toast({
                    color: 'success',
                    content: emp.language['gx6008-000052'] // 操作成功
                });
            }
        }
        catch(e) {

        }
    }

    // 获取offer内容
    getOfferContent = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/getOfferModalContent',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                return res.data;
            }
        }
        catch(e) {
        }
    }
    
    // 接受offer
    acceptOffer = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/acceptOffer',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                this.toast({
                    color: 'success',
                    content: emp.language['gx6008-000052'] // 操作成功
                });
            }
        }
        catch(e) {
        }
    }

    // 拒绝offer
    rejectOffer = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/rejectOffer',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                this.toast({
                    color: 'success',
                    content: emp.language['gx6008-000052'] // 操作成功
                });
            }
        }
        catch(e) {
        }
    }

    // 提交offer
    submitOffer = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/submitOffer',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                this.toast({
                    color: 'success',
                    content: emp.language['gx6008-000052'] // 操作成功
                });
            }
        }
        catch(e) {
        }
    }
}